package com.alibaba.springboot.health.controller;

import com.alibaba.springboot.health.pojo.Doctor;
import com.alibaba.springboot.health.service.DoctorService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (Doctor)表控制层
 *
 * @author makejava
 * @since 2019-12-27 09:56:12
 */
@Controller
public class DoctorController {
    /**
     * 服务对象
     */
    @Autowired
    private DoctorService doctorService;

    /**
     * zdj
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/doctor/searchOne")
    public String searchOne(Integer id,Model model) {
        Doctor doctor = doctorService.queryById(id);
        model.addAttribute("doctor",doctor);
        return "doctor-edit";
    }
    
    
    /**
     *zdj
     * 权限  后台用户  doctoradmin
     * 查询多条数据
     * @param page 页码
     * @param pageSize 页大小
     * @param doctor 实体
     * @return PageInfo<Doctor> 分页数据集合
     */
    @RequiresPermissions(value = {"/doctor/searchAll"})
    @RequestMapping("/doctor/searchAll")
    public String searchAll(@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "pageSize",defaultValue = "5")int pageSize, Doctor doctor, Model model){
        PageInfo<Doctor> doctors = doctorService.queryAll(page, pageSize, doctor);
        model.addAttribute("doctors",doctors);
        return "member-doctor";
    }

    /**
     *
     * @param doctor
     * @return
     */
    @RequestMapping("/doctor/update")
    @ResponseBody
    public Doctor update(Doctor doctor){
        Doctor update = doctorService.update(doctor);
        return update;
    }

    /**
     * jrl
     * @param model
     * @return
     */
    @RequestMapping("/allDoctor")
    public String allDoctor(Model model ){
        List<Doctor> doctors = doctorService.selectAllDoctor();
        model.addAttribute("doctorList",doctors);
        return "doctor_list";
    }

    /**
     * jrl
     * @param id
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/gouMai")
    public String gouMai(@RequestParam("id") int id, Model model, HttpServletRequest request){
        Doctor doctor = doctorService.queryById(id);
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        model.addAttribute("doctor",doctor);
        model.addAttribute("user", user);
        return"goumai";
    }

    /**
     * jrl
     * @param doctorName
     * @param tel
     * @param name
     * @param sex
     * @param age
     * @param model
     * @param price
     * @return
     */
    @RequestMapping(value = "/dingdian",method = RequestMethod.GET)
    public String dingdian(String doctorName,String tel,String name,String sex,int age,Model model,double price){
        model.addAttribute("doctorName",doctorName);
        model.addAttribute("tel",tel);
        model.addAttribute("name",name);
        model.addAttribute("sex",sex);
        model.addAttribute("age",age);
        model.addAttribute("price",price);
        return"qrddy";
    }

    /**
     * jrl
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("selectOne")
    public String selectOne(@RequestParam("id") Integer id,Model model) {
        Doctor doctor = doctorService.queryById(id);
        model.addAttribute("doctor",doctor);
        return "doctor_show";
    }
}